package com.ruoyi.an.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.an.domain.*;
import com.ruoyi.an.service.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.an.mapper.AnCallBackMapper;

import static com.ruoyi.an.util.AnPlatformUtil.requestNewPlatformQuestionReport;

/**
 * 京师平台端回调结果Service业务层处理
 *
 * @author liuzheng
 * @date 2022-11-13
 */
@Service
public class AnCallBackServiceImpl implements IAnCallBackService {
    @Autowired
    private AnCallBackMapper anCallBackMapper;
    @Value("${ruoyi.profile}")
    private String rootPath;
    @Value("${ruoyi.evaluationHost}")
    private String evaluationHost;
    @Value("${ruoyi.evaluationWebViewHost}")
    private String evaluationWebViewHost;
    @Autowired
    private IAnWechatUserService anWechatUserService;
    @Autowired
    private IAnMechanismService anMechanismService;
    @Autowired
    private IAnWechatUserMechanismService anWechatUserMechanismService;
    @Autowired
    private IAnMechanismMemberService anMechanismMemberService;
    @Autowired
    private IAnMechanismSettlementService anMechanismSettlementService;
    @Autowired
    private IAnMechanismSettlementInfoService anMechanismSettlementInfoService;

    /**
     * 查询京师平台端回调结果
     *
     * @param tradeNo 京师平台端回调结果主键
     * @return 京师平台端回调结果
     */
    @Override
    public AnCallBack selectAnCallBackByTradeNo(String tradeNo) {
        return anCallBackMapper.selectAnCallBackByTradeNo(tradeNo);
    }

    /**
     * 查询京师平台端回调结果列表
     *
     * @param anCallBack 京师平台端回调结果
     * @return 京师平台端回调结果
     */
    @Override
    public List<AnCallBack> selectAnCallBackList(AnCallBack anCallBack) {
        return anCallBackMapper.selectAnCallBackList(anCallBack);
    }

    /**
     * 新增京师平台端回调结果
     *
     * @param anCallBack 京师平台端回调结果
     * @return 结果
     */
    @Override
    public int insertAnCallBack(AnCallBack anCallBack) {
        anCallBack.setCreateTime(DateUtils.getNowDate());
        return anCallBackMapper.insertAnCallBack(anCallBack);
    }

    /**
     * 修改京师平台端回调结果
     *
     * @param anCallBack 京师平台端回调结果
     * @return 结果
     */
    @Override
    public int updateAnCallBack(AnCallBack anCallBack) {
        anCallBack.setUpdateTime(DateUtils.getNowDate());
        return anCallBackMapper.updateAnCallBack(anCallBack);
    }

    /**
     * 批量删除京师平台端回调结果
     *
     * @param tradeNos 需要删除的京师平台端回调结果主键
     * @return 结果
     */
    @Override
    public int deleteAnCallBackByTradeNos(String[] tradeNos) {
        return anCallBackMapper.deleteAnCallBackByTradeNos(tradeNos);
    }

    /**
     * 删除京师平台端回调结果信息
     *
     * @param tradeNo 京师平台端回调结果主键
     * @return 结果
     */
    @Override
    public int deleteAnCallBackByTradeNo(String tradeNo) {
        return anCallBackMapper.deleteAnCallBackByTradeNo(tradeNo);
    }

    @Override
    public JSONObject refreshUserReport(JSONObject requestObj) {
        //订单号
        try {
            String tradeNo = requestObj.getString("tradeNo");
            String outTradeNo = requestObj.getString("outTradeNo");
            AnCallBack callBack = new AnCallBack();
            callBack.setOutTradeNo(outTradeNo);
            callBack.setTradeNo(tradeNo);
            callBack.setCallBakcStr(requestObj.toJSONString());
            callBack.setCreateBy("admin");
            callBack.setCreateTime(DateUtils.getNowDate());
            insertAnCallBack(callBack);
            AnWechatUserMechanism tempAnWechatUserMechanism = new AnWechatUserMechanism();
            tempAnWechatUserMechanism.setTradeNo(tradeNo);
            tempAnWechatUserMechanism.setOutTradeNo(outTradeNo);
            JSONObject object = requestNewPlatformQuestionReport(tempAnWechatUserMechanism);
            //测评分数
            long screeningScore = 0L;
            if (object.getInteger("code") == 1) {
                JSONObject data = object.getJSONObject("data");
                screeningScore = data.getLong("score");
            }
            //测评结果
            AnWechatUserMechanism anWechatUserMechanism = new AnWechatUserMechanism();
            anWechatUserMechanism.setOutTradeNo(outTradeNo);
            List<AnWechatUserMechanism> anWechatUserMechanisms = anWechatUserMechanismService.selectAnWechatUserMechanismList(anWechatUserMechanism);
            for (AnWechatUserMechanism wechatUserMechanism : anWechatUserMechanisms) {
                String errorInfo = object.toJSONString();
                if (errorInfo.indexOf("无效的用户凭证")==-1)
                {
                    wechatUserMechanism.setIsDone(0L);
                    wechatUserMechanism.setScreeningScore(screeningScore);
                    wechatUserMechanism.setScreeningResult(object.toJSONString());
                    wechatUserMechanism.setScreeningDoneTime(DateUtils.getNowDate());
                    wechatUserMechanism.setIsMechanism(0L);
                    anWechatUserMechanismService.updateAnWechatUserMechanism(wechatUserMechanism);
                }
                //查询推广结算表，如果没有就证明是第一次完成测评，插入结算记录
                AnMechanismSettlementInfo anMechanismSettlementInfo = new AnMechanismSettlementInfo();
                anMechanismSettlementInfo.setWechatUserId(wechatUserMechanism.getWechatUserId());
                List<AnMechanismSettlementInfo> anMechanismSettlementInfos = anMechanismSettlementInfoService.selectAnMechanismSettlementInfoList(anMechanismSettlementInfo);
                if (anMechanismSettlementInfos.size() == 0) {
                    //查询到最近的佣金匹配规则
                    AnMechanismSettlementInfo addAnMechanismSettlementInfo = new AnMechanismSettlementInfo();
                    addAnMechanismSettlementInfo.setMechanismMemberUserId(wechatUserMechanism.getMechanismMemberUserId());
                    addAnMechanismSettlementInfo.setWechatUserId(wechatUserMechanism.getWechatUserId());
                    addAnMechanismSettlementInfo.setSettlementStatus(1L);
                    addAnMechanismSettlementInfo.setCreateBy("admin");
                    addAnMechanismSettlementInfo.setCreateTime(DateUtils.getNowDate());
                    List<AnMechanismSettlement> matchMechanismSettlements = anMechanismSettlementService.selectOutTradeNoSettlementList(outTradeNo);
                    if (matchMechanismSettlements.size() > 0) {
                        addAnMechanismSettlementInfo.setMechanismSettlementId(matchMechanismSettlements.get(0).getMechanismSettlementId());
                        addAnMechanismSettlementInfo.setSettlementPrice(matchMechanismSettlements.get(0).getSettlementPrice());
                    }
                    anMechanismSettlementInfoService.insertAnMechanismSettlementInfo(addAnMechanismSettlementInfo);
                }
                //修改用户信息
                AnWechatUser anWechatUser = anWechatUserService.selectAnWechatUserByWechatUserId(wechatUserMechanism.getWechatUserId());
                if(!StringUtils.isNotEmpty(anWechatUser.getScreeningSource())){
                    AnMechanismMember anMechanismMember = anMechanismMemberService.selectAnMechanismMemberByMechanismMemberId(wechatUserMechanism.getMechanismMemberId());
                    if(anMechanismMember!=null){
                        AnMechanism anMechanism = anMechanismService.selectAnMechanismByMechanismId(anMechanismMember.getMechanismId());
                        anWechatUser.setScreeningSource(anMechanism.getMechanismName() + "-" + anMechanismMember.getMemberName());
                        anWechatUser.setMechanismId(anMechanismMember.getMechanismId());
                        anWechatUser.setMechanismMemberId(anMechanismMember.getMechanismMemberId());
                    }
                }
                anWechatUser.setScreeningScore(screeningScore);
                anWechatUser.setIsDone(0L);
                anWechatUser.setAuthTime(DateUtils.getNowDate());
                anWechatUserService.updateAnWechatUser(anWechatUser);
            }
        } catch (Exception e) {
//            e.printStackTrace();
            requestObj.put("msg", "请勿重复提交！");
            return requestObj;
        }
        return requestObj;
    }
}
